package l1j.server.server.clientpackets;

import l1j.server.server.datatables.CustomerServiceTable;
import l1j.server.server.model.L1World;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.serverpackets.S_NPCTalkReturn;
import l1j.server.server.serverpackets.S_Party;

public class C_CustomerService{
	
	private static C_CustomerService _instance;
	
	public static C_CustomerService getInstance(String s, L1PcInstance pc) {
		_instance = new C_CustomerService(s, pc);
		return _instance;
	}
	public C_CustomerService(String s, L1PcInstance pc){

		CustomerServiceTable CST = new CustomerServiceTable();
		if (s.equalsIgnoreCase("callgm")){
			int caseid = pc.getId();
			boolean csing = CST.isCharInQueue(caseid);
			String htmlid = null;
			if (csing){
				int csq = CST.loadQueueNum(caseid);
				htmlid = "cs_queue"+csq;				
				pc.sendPackets(new S_NPCTalkReturn(pc.getId(), htmlid));
			} else {
				CST.storeQueueNum(caseid, pc.getName(), 1);
				int csq = CST.loadQueueNum(caseid);
				htmlid = "cs_queue"+csq;				
				pc.sendPackets(new S_NPCTalkReturn(pc.getId(), htmlid));					
			}
		}
		
		else if (s.equalsIgnoreCase("support")){
			int caseid = pc.getId();
			boolean csing = CST.isCharInQueue(caseid);
			String htmlid = null;
			if (csing){
				int csq = CST.loadQueueNum(caseid);
				htmlid = "cs_queue"+csq;				
				pc.sendPackets(new S_NPCTalkReturn(pc.getId(), htmlid));
			} else {
				CST.storeQueueNum(caseid, pc.getName(), 0);
				int csq = CST.loadQueueNum(caseid);
				htmlid = "cs_queue"+csq;				
				pc.sendPackets(new S_NPCTalkReturn(pc.getId(), htmlid));					
			}
			CST.updatePcQueueList();
		}
		
		else if (s.equalsIgnoreCase("delqueue")){
			CST.deleteQueueNum(pc.getId());
			pc.sendPackets(new S_NPCTalkReturn(pc.getId(), ""));
			CST.updatePcQueueList();
		}
		
		else if (s.equalsIgnoreCase("startcs") && pc.isGm()){
			CST.loadQueueList();
			int gmid = pc.getId();
			String gmname = pc.getName();
			int playerid = CST.getCaseID();
			String playername = CST.getName();
			L1PcInstance PLAYER = L1World.getInstance().getPlayer(playername);
			
			PLAYER.sendPackets(new S_Party("cs_chat", playerid, gmname, "您好，"+gmname+"很高興為您服務"));
			pc.sendPackets(new S_Party("cs_gmchat", gmid, playername, "開始與"+playername+"對話"));
			
			CST.updateGMBusy(gmid, true);
			CST.updateGMTarget(gmid, playerid, playername);
			PLAYER.setBusy(1);
			pc.setBusy(1);
			
			CST.updateQueuestatus(playerid, "chating");
			CST.updatePcQueueList();
		}
		
		else if (s.equalsIgnoreCase("endcs")){
			int pcId = pc.getId();
			CST.loadPCtoGM(pcId);
			String gmname = CST.getGMName();
			L1PcInstance gm = L1World.getInstance().getPlayer(gmname);
			int gmId = gm.getId();
			pc.sendPackets(new S_NPCTalkReturn(pc.getId(), "cs_end"));
			
			CST.updateGMBusy(gmId, false);
			gm.setBusy(0);
			pc.setBusy(2);
			
			CST.UpdateStatus(pcId);
		}
		
		else if (s.equalsIgnoreCase("endcsbygm") && pc.isGm()){
			CST.loadGMtoPC(pc.getId());
			int pcId = CST.getPCId();
			int gmId = pc.getId();
			String playername = CST.getPCName();
			L1PcInstance PLAYER = L1World.getInstance().getPlayer(playername);
			PLAYER.sendPackets(new S_NPCTalkReturn(pcId, "cs_end"));
			
			CST.updateGMBusy(gmId, false);
			PLAYER.setBusy(2);
			pc.setBusy(0);
			
			CST.UpdateStatus(pcId);
		}
		
		else if (s.equalsIgnoreCase("markone")){
			int pcId = pc.getId();
			CST.loadPCtoGM(pcId);
			int gmId = CST.getGMId();

			pc.sendPackets(new S_NPCTalkReturn(pc.getId(), "cs_markone"));
			
			CST.UpdateManner(gmId, 20);
			pc.setBusy(0);
			
			CST.deleteQueueNum(pcId);
			CST.UpdateStatus(pcId);
			CST.updateGMTarget(gmId, 0, "null");
		}
		
		else if (s.equalsIgnoreCase("marktwo")){
			int pcId = pc.getId();
			CST.loadPCtoGM(pcId);
			int gmId = CST.getGMId();
			
			pc.sendPackets(new S_NPCTalkReturn(pc.getId(), "cs_marktwo"));
			
			CST.UpdateManner(gmId, 40);
			pc.setBusy(0);
			
			CST.deleteQueueNum(pcId);
			CST.UpdateStatus(pcId);
			CST.updateGMTarget(gmId, 0, "null");
		}
		
		else if (s.equalsIgnoreCase("markthree")){
			int pcId = pc.getId();
			CST.loadPCtoGM(pcId);
			int gmId = CST.getGMId();
			
			pc.sendPackets(new S_NPCTalkReturn(pc.getId(), "cs_markthree"));
			
			CST.UpdateManner(gmId, 60);
			pc.setBusy(0);
			
			CST.deleteQueueNum(pcId);
			CST.UpdateStatus(pcId);
			CST.updateGMTarget(gmId, 0, "null");
		}
		
		else if (s.equalsIgnoreCase("markfour")){
			int pcId = pc.getId();
			CST.loadPCtoGM(pcId);
			int gmId = CST.getGMId();
			
			pc.sendPackets(new S_NPCTalkReturn(pc.getId(), "cs_markfour"));
			
			CST.UpdateManner(gmId, 80);
			pc.setBusy(0);
			
			CST.deleteQueueNum(pcId);
			CST.UpdateStatus(pcId);
			CST.updateGMTarget(gmId, 0, "null");
		}
		
		else if (s.equalsIgnoreCase("markfive")){
			int pcId = pc.getId();
			CST.loadPCtoGM(pcId);
			int gmId = CST.getGMId();
			
			pc.sendPackets(new S_NPCTalkReturn(pc.getId(), "cs_markfive"));
			
			CST.UpdateManner(gmId, 100);
			pc.setBusy(0);
			
			CST.deleteQueueNum(pcId);
			CST.UpdateStatus(pcId);
			CST.updateGMTarget(gmId, 0, "null");
		}
		
		else if (s.equalsIgnoreCase("finallyend")){
			pc.sendPackets(new S_NPCTalkReturn(pc.getId(), ""));
		}
	}
}
